package com.ys.ylideas.schedule;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.ys.ylideas.model.CreativeTypeResultReport;
import com.ys.ylideas.model.KeyWordTypeResultReport;
import com.ys.ylideas.model.SearchWordTypeResultReport;
import com.ys.ylideas.service.AccountService;
import com.ys.ylideas.service.CreativeReportService;
import com.ys.ylideas.service.KeyWordReportService;
import com.ys.ylideas.service.SearchWordReportService;
import com.ys.ylideas.util.CommonData;
import com.ys.ylideas.util.HttpRequest;


@Component
public class BackUpReportTask {
	
	private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Autowired
	private AccountService accountService;
	@Autowired
	private KeyWordReportService keyWordReportService;
	@Autowired
	private SearchWordReportService searchWordReportService;
	@Autowired
	private CreativeReportService creativeReportService;
	
	// 23点30分执行
	@Scheduled(cron="30 30 00 * * ?")	
	public void BackUpCreativeAndReportTask(){
		Date date = new Date();
		CommonData.logger.info("开始备份百度用户数据： " + sdf.format(date));
		
		List<Long> userIdList = accountService.getAllUserId();
		CommonData.logger.info("获取用户数目："+userIdList.size());
		for(long userId:userIdList){
			String rfToken = accountService.getUserToken(userId);
			List<String> result = HttpRequest.getAccessToken(rfToken);
			if (result.size() != 0) {
				String accessToken = result.get(0);
				String reToken = result.get(1);
				if(accessToken!=null&&!accessToken.equals("")&&reToken!=null&&!reToken.equals("")){
					Map<String,Object> map = new HashMap<>();
					map.put("refreshToken", reToken);
					map.put("userId", userId);
					accountService.updateRFTokenByUserId(map);
					boolean size = saveKeyWordReport(accessToken,userId);
					if (size) {
						CommonData.logger.debug("用户："+userId+"成功获取关键词报告");
					}else{
						CommonData.logger.debug("用户："+userId+"获取不到关键词报告");
					}
					
					size = saveSearchWordReport(accessToken,userId);
					if (size) {
						CommonData.logger.debug("用户："+userId+"成功获取搜索词报告");
					}else{
						CommonData.logger.debug("用户："+userId+"获取不到搜索词报告");
					}
					
					size = saveCreativeReport(accessToken,userId);
					if (size) {
						CommonData.logger.debug("用户："+userId+"成功获取创意报告");
					}else{
						CommonData.logger.debug("用户："+userId+"获取不到创意报告");
					}
				}
			}
		}

	}
	
	public boolean saveKeyWordReport(String accessToken,long userId){
		try{
			Map<String, Object> map = HttpRequest.getKeyWordReport(accessToken,userId);
			List<KeyWordTypeResultReport> ktrrList = (List<KeyWordTypeResultReport>) map.get("ktrrList");
			CommonData.logger.debug("kttrList = "+ktrrList.size());
			keyWordReportService.batchInsert(ktrrList);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	public boolean saveSearchWordReport(String accessToken,long userId){
		try{
			Map<String, Object> map = HttpRequest.getSearchWordReport(accessToken,userId);
			List<SearchWordTypeResultReport> swtrList = (List<SearchWordTypeResultReport>) map.get("swtrList");
			CommonData.logger.debug("swtrList = "+swtrList.size());
			searchWordReportService.batchInsert(swtrList);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	
	public boolean saveCreativeReport(String accessToken,long userId){
		try{
			Map<String, Object> map = HttpRequest.getCreativeReport(accessToken,userId);
			List<CreativeTypeResultReport> ctrrList = (List<CreativeTypeResultReport>) map.get("ctrrList");
			CommonData.logger.debug("ctrrList = "+ctrrList.size());
			creativeReportService.batchInsert(ctrrList);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
}
